import * as types from './types';

const initialState = {
  deviceId: null,
  devName: 'No connect',
  allData: [],
  kmStr: 'Miles',
  nameStr: 'Rawrr_Name',
  headStr: '',
  hadUpdate: false
};

const reducer = (state = initialState, action) => {
  switch (action.type) {
    // 连接
    case types.CONNECTDEV: {
      console.log('CONNECTDEV');
      const { devInfo } = action.payload;

      return {
        ...state,
        deviceId: devInfo.uid,
        devName: devInfo.name,
      };
    }

    // 断开连接
    case types.DISCONNECTDEV: {
      return {
        // ...initialState,
        ...state,
        deviceId: null,
        devName: '',
      };
    }

    case types.UPDATEALLDATA: {
      // allData = [];
      const { allData } = action.payload;
      return {
        // ...initialState,
        ...state,
        allData: [...allData],
      };
    }

    // 修改KM显示
    case types.CHANGEKM: {
      const { kmStr } = action.payload;

      return {
        ...state,
        kmStr,
      };
    }

    // 修改NAME
    case types.CHANGENAME: {
      const { nameStr } = action.payload;

      return {
        ...state,
        nameStr,
      };
    }

    // 修改头像Head
    case types.CHANGEHEAD: {
      const { headStr } = action.payload;

      return {
        ...state,
        headStr,
      };
    }


    // 进行过更新
    case types.CHANGEHADUPDATE: {
      const { hadUpdate } = action.payload;
      return {
        ...state,
        hadUpdate,
      };
    }

    default: {
      return state;
    }
  }
};

export default reducer;
